package borge.web.teamspirit.client.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import borge.web.teamspirit.client.interfaces.PersistentData;

/**
 * A league represents a sport during a certain season.
 * The league can be "owned" by a registered Association, but it
 * doesn't have to be. If the app is running in free mode, the league
 * will be created and associated with a Serie that a Team creates.
 * However, if it is owned by an association, the association creates
 * League entities for the various sports and seasons they control.
 * A League also consists of a list of divisions that a team plays in.
 * A Serie entity that is created by a Team is mapped to a League and
 * one of its divisions.
 * 
 * Example: LUGI, Herrar Innefotboll HT09
 * @author Andreas Borglin
 *
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class League implements PersistentData {

	public static enum Category{
		MEN,
		WOMEN,
		MIXED
	}

	/*
	public static enum Sport{
		SOCCER,
		HOCKEY,
		BANDY,
		FLOORBALL,
		HANDBALL,
		VOLLEYBALL
		
	}*/
	
	public static enum Type{
		SERIE,
		CUP
	}
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String iEncodedKey;	
	
	// Owning association
	@Persistent
	private String iAssociationKey;
	
	@Persistent
	private String iSportKey;
	
	@Persistent
	private String iSeason;
	
	// Examples: Men, Women, Junior, etc
	@Persistent
	private Category iCategory;
	
	
	// dates in division instead??
	@Persistent
	private Date iStartDate;
	
	@Persistent
	private Date iEndDate;

	// should this be owned or not?
	@Persistent
	private List<LeagueDivision> iDivisions;
	
	// For testing only
	@NotPersistent
	public String iAssociationName;
	@NotPersistent
	public String iSportName;
	
	@NotPersistent
	private Sport iSport;
	
	public League(){
		iStartDate = new Date();
		iEndDate = new Date();
		iDivisions = new ArrayList<LeagueDivision>();
	}
	
	public League(String s, Category c){
		this();
		iSeason = s;
		iCategory = c;
	}

	public String toString(){
		return iSportName + " " + iSeason;
	}
	
	public void setSportName(String aName){
		iSportName = aName;
	}
	
	public String getEncodedKey(){
		return iEncodedKey;
	}
	
	public void setAssociationKey(String aKey){
		iAssociationKey = aKey;
	}
	
	public String getAssociationKey(){
		return iAssociationKey;
	}
	
	public void setSeason(String aSeason){
		iSeason = aSeason;
	}
	
	public String getSeason(){
		return iSeason;
	}
	
	public void setSportKey(String aKey){
		iSportKey = aKey;
	}
	
	public String getSportKey(){
		return iSportKey;
	}

	public void setCategory(Category aCategory) {
		this.iCategory = aCategory;
	}

	public Category getCategory() {
		return iCategory;
	}


    public void setStartDate(Date aStartDate) {
        this.iStartDate = aStartDate;
    }

    public Date getStartDate() {
        return iStartDate;
    }

    public void setEndDate(Date aEndDate) {
        this.iEndDate = aEndDate;
    }

    public Date getEndDate() {
        return iEndDate;
    }

	public void addDivision(LeagueDivision aDiv){
		iDivisions.add(aDiv);
	}
	
	public List<LeagueDivision> getDivisions(){
		return iDivisions;
	}
	
	public int getNumDivisions(){
		return iDivisions.size();
	}
	
	public LeagueDivision getDivision(int index){
		return iDivisions.get(index);
	}
	
	public void setSport(Sport sport){
		iSport = sport;
	}
	
	public Sport getSport(){
		return iSport;
	}
    
	@Override
	public String getDisplayingWidget() {
		// TODO Auto-generated method stub
		return null;
	}

	
	
	// en liga som tex innefotboll ht09
	
	// kopplad till en sport, tex innefotboll
	// vara mappad mot serie som tex innefotboll ht09 hos lugi
	// inneh�lla ett startdatum och slutdatum f�r registreringar till denna liga
	// b�r �ven inneh�lla en lista �ver alla divisioner som finns f�r ligan, intern class f�r detta?
	
	// automatiskt h�mta dessa fr�n lugi?
	
	// lag kan sen skapa en serie som �r mappat till en liga och division
	
}
